package com.cch.stock.task;

import com.cch.utils.SysUtil;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by peter.zhu on 2015/7/22.
 * 代替 spring xml 配置
 */
@Configuration
@ComponentScan("com.cch.stock.task")
@EnableScheduling
public class TaskConfg implements SchedulingConfigurer {
    private static final Logger logger = Logger.getLogger(TaskConfg.class);

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (SysUtil.isScheduler()) {
            taskRegistrar.setScheduler(taskScheduler());
            logger.info("===========TaskConfg this Tomcat Scheduler is On!!");
        }else {
            taskRegistrar.setCronTasksList(null);
            taskRegistrar.setFixedDelayTasksList(null);
            taskRegistrar.setFixedRateTasksList(null);
            taskRegistrar.setTriggerTasksList(null);
            logger.info("===========TaskConfg this Tomcat Scheduler is Off!!");
        }
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskScheduler() {
        return Executors.newScheduledThreadPool(30);
    }

    @Bean
    public TaskWorker myTaskWorker() {
        return new TaskWorker();
    }
}
